package com.dqyt.khjc.service;


import com.dqyt.app.bean.HomeBean;
import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.mapper.LoginHistoryMapper;
import com.dqyt.khjc.mapper.RoleMapper;
import com.dqyt.khjc.mapper.UserMapper;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 登录service
 */
@Service
public class HomeService {

    @Autowired
    private UserMapper mUserMapper;

    @Autowired
    private LoginHistoryMapper mLoginHistoryMapper;

    @Autowired
    private RoleMapper mRoleMapper;


    @Autowired
    private SampleDao mSampleDao;

    @Autowired
    private ReportDao mReportDao;

    /**
     * 查询首页数据
     *
     * @param request
     * @return
     */
    public ResultMsg home(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        Jedis jedis = null;

        try {

            // 检查用户权限信息是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            if (ret != null) { // 如果权限检查错误
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            //获取当天零点整的时间 0:00:00
            long dayZero = TimeUtil.getCurrentDayZero();

            //获取当天的23:59:59
            long todayEndTime = TimeUtil.getTodayEndTime();


            //获取1天前的时间戳
            long befault1day = TimeUtil.getDateBefore(new Date(todayEndTime), 1);
            //获取7天前的时间戳
            long befault7day = TimeUtil.getDateBefore(new Date(todayEndTime), 7);
            //获取14天前的时间戳
            long befault14day = TimeUtil.getDateBefore(new Date(todayEndTime), 14);
            //获取30天前的时间戳
            long befault30day = TimeUtil.getDateBefore(new Date(todayEndTime), 30);
            //获取60天前的时间戳
            long befault60day = TimeUtil.getDateBefore(new Date(todayEndTime), 60);





            //查询昨天新增的呼吸器数量
            //int hxqAdd1day = mSampleDao.befaultxDate(befault1day, Sample.YP_TYPE_HXQ);
            //查询7日新增的呼吸器数量
            //int hxqAdd7day = mSampleDao.befaultxDate(befault7day, Sample.YP_TYPE_HXQ);
            //查询14天前新增的呼吸器数量
            //int hxqAdd14day = mSampleDao.befaultxDate(befault14day, Sample.YP_TYPE_HXQ);
            //查询30天前新增的呼吸器数量
            //int hxqAdd30day = mSampleDao.befaultxDate(befault30day, Sample.YP_TYPE_HXQ);
            //查询60天前新增的呼吸器数量
            List<Sample> hxqAdd60dayArr = mSampleDao.befaultxDate(befault60day, Sample.YP_TYPE_HXQ);

            int hxqAdd1day =0;
            int hxqAdd7day =0;
            int hxqAdd14day =0;
            int hxqAdd30day =0;
            int hxqAdd60day =hxqAdd60dayArr.size();
            for (Sample sample : hxqAdd60dayArr) {
                if(sample.getAddTime()>befault1day){
                    hxqAdd1day++;
                }
                if(sample.getAddTime()>befault7day){
                    hxqAdd7day++;
                }
                if(sample.getAddTime()>befault14day){
                    hxqAdd14day++;
                }
                if(sample.getAddTime()>befault30day){
                    hxqAdd30day++;
                }
            }


            //查询昨天新增的复合气瓶数量
            //int fhqpAdd1day = mSampleDao.befaultxDate(befault1day, Sample.YP_TYPE_FHQP);
            //查询7日新增的复合气瓶数量
            //int fhqpAdd7day = mSampleDao.befaultxDate(befault7day, Sample.YP_TYPE_FHQP);
            //查询14天前新增的复合气瓶数量
            //int fhqpAdd14day = mSampleDao.befaultxDate(befault14day, Sample.YP_TYPE_FHQP);
            //查询30天前新增的复合气瓶数量
            //int fhqpAdd30day = mSampleDao.befaultxDate(befault30day, Sample.YP_TYPE_FHQP);

            //查询60天前新增的复合气瓶数量
            List<Sample> fhqpAdd60dayArr = mSampleDao.befaultxDate(befault60day, Sample.YP_TYPE_FHQP);

            int fhqpAdd1day=0;
            int fhqpAdd7day =0;
            int fhqpAdd14day= 0;
            int fhqpAdd30day = 0;
            int fhqpAdd60day =fhqpAdd60dayArr.size();
            for (Sample sample : fhqpAdd60dayArr) {
                //得到昨天的复合气瓶数量
                if(sample.getAddTime()>befault1day){
                    fhqpAdd1day++;
                }
                //得到7日新增的复合气瓶数量
                if(sample.getAddTime()>befault7day){
                    fhqpAdd7day++;
                }
                //得到 14天前新增的复合气瓶数量
                if(sample.getAddTime()>befault14day){
                    fhqpAdd14day++;
                }
                //得到30天前新增的复合气瓶数量
                if(sample.getAddTime()>befault30day){
                    fhqpAdd30day++;
                }
            }


            //查询昨天新增报告数量
            //int reportAdd1day = mReportDao.befaultxDate(befault1day);
            //查询7日新增的报告数量
            //int reportAdd7day = mReportDao.befaultxDate(befault7day);
            //查询30日新增的报告数量
            //int reportAdd30day = mReportDao.befaultxDate(befault30day);
            //查询60天前新增的报告数量
            List<Report> reportAdd60dayArr = mReportDao.befaultxDate(befault60day);

            int reportAdd1day=0;
            int reportAdd7day =0;
            int reportAdd30day =0;
            int reportAdd60day = reportAdd60dayArr.size();
            for (Report report : reportAdd60dayArr) {
                //得到昨天新增报告数量
                if(report.getAddTime()>befault1day){
                    reportAdd1day++;
                }
                //得到7日新增的报告数量
                if(report.getAddTime()>befault7day){
                    reportAdd7day++;
                }
                //得到30日新增的报告数量
                if(report.getAddTime()>befault30day){
                    reportAdd30day++;
                }
            }


            //查询昨日审核报告
            //查询昨日已完成报告
            //int ywcAdd1day = mReportDao.befaultxDateSh(befault1day);
            //查询7日新增的已完成报告
            //int ywcAdd7day = mReportDao.befaultxDateSh(befault7day);
            //查询30日新增已完成报告
            //int ywcAdd30day = mReportDao.befaultxDateSh(befault30day);
            //查询60天前新增的已完成报告
            List<Report> ywcAdd60dayArr = mReportDao.befaultxDateSh(befault60day);


            int ywcAdd1day =0;
            int ywcAdd7day =0;
            int ywcAdd30day =0;
            int ywcAdd60day =ywcAdd60dayArr.size();

            for (Report report : ywcAdd60dayArr) {
                //得到昨日已完成报告
                if(report.getAddTime()>befault1day){
                    ywcAdd1day++;
                }
                //得到7日新增的已完成报告
                if(report.getAddTime()>befault7day){
                    ywcAdd7day++;
                }
                //得到30日新增已完成报告
                if(report.getAddTime()>befault30day){
                    ywcAdd30day++;
                }
            }

            //最近15天接收样品数量
            long tadays = TimeUtil.getCurrentDayZero();
            Date date = new Date();
            date.setTime(tadays);


            long bfday1 = TimeUtil.getDateBefore(date, 1);
            long bfday2 = TimeUtil.getDateBefore(date, 2);
            long bfday3 = TimeUtil.getDateBefore(date, 3);
            long bfday4 = TimeUtil.getDateBefore(date, 4);
            long bfday5 = TimeUtil.getDateBefore(date, 5);
            long bfday6 = TimeUtil.getDateBefore(date, 6);
            long bfday7 = TimeUtil.getDateBefore(date, 7);
            long bfday8 = TimeUtil.getDateBefore(date, 8);
            long bfday9 = TimeUtil.getDateBefore(date, 9);
            long bfday10 = TimeUtil.getDateBefore(date, 10);
            long bfday11 = TimeUtil.getDateBefore(date, 11);
            long bfday12 = TimeUtil.getDateBefore(date, 12);
            long bfday13 = TimeUtil.getDateBefore(date, 13);
            long bfday14 = TimeUtil.getDateBefore(date, 14);
            long bfday15 = TimeUtil.getDateBefore(date, 15);
            //计算N天后的时间毫秒值
            String day01 = TimeUtil.formatTime_5(bfday1);
            String day02 = TimeUtil.formatTime_5(bfday2);
            String day03 = TimeUtil.formatTime_5(bfday3);
            String day04 = TimeUtil.formatTime_5(bfday4);
            String day05 = TimeUtil.formatTime_5(bfday5);
            String day06 = TimeUtil.formatTime_5(bfday6);
            String day07 = TimeUtil.formatTime_5(bfday7);
            String day08 = TimeUtil.formatTime_5(bfday8);
            String day09 = TimeUtil.formatTime_5(bfday9);
            String day10 = TimeUtil.formatTime_5(bfday10);
            String day11 = TimeUtil.formatTime_5(bfday11);
            String day12 = TimeUtil.formatTime_5(bfday12);
            String day13 = TimeUtil.formatTime_5(bfday13);
            String day14 = TimeUtil.formatTime_5(bfday14);
            String day15 = TimeUtil.formatTime_5(bfday15);


            //1天前
            //int smpday1 = mSampleDao.befaultxDate2(bfday1);
            //1天前
            //int smpday2 = mSampleDao.befaultxDate2(bfday2);
            //1天前
            //int smpday3 = mSampleDao.befaultxDate2(bfday3);
            //1天前
            //int smpday4 = mSampleDao.befaultxDate2(bfday4);
            //1天前
            //int smpday5 = mSampleDao.befaultxDate2(bfday5);
            //1天前
            //int smpday6 = mSampleDao.befaultxDate2(bfday6);
            //1天前
            //int smpday7 = mSampleDao.befaultxDate2(bfday7);
            //1天前
            //int smpday8 = mSampleDao.befaultxDate2(bfday8);
            //1天前
            //int smpday9 = mSampleDao.befaultxDate2(bfday9);
            //1天前
            //int smpday10 = mSampleDao.befaultxDate2(bfday10);
            //1天前
            //int smpday11 = mSampleDao.befaultxDate2(bfday11);
            //1天前
            //int smpday12 = mSampleDao.befaultxDate2(bfday12);
            //1天前
            //int smpday13 = mSampleDao.befaultxDate2(bfday13);
            //1天前
            //int smpday14 = mSampleDao.befaultxDate2(bfday14);
            //1天前
            List<Sample> smpday15Arr = mSampleDao.befaultxDate2(bfday15);

            int smpday1 =0;
            int smpday2 =0;
            int smpday3 =0;
            int smpday4 =0;
            int smpday5 =0;
            int smpday6 =0;
            int smpday7 =0;
            int smpday8 =0;
            int smpday9 =0;
            int smpday10 =0;
            int smpday11 =0;
            int smpday12 =0;
            int smpday13 =0;
            int smpday14 =0;
            int smpday15 =smpday15Arr.size();
            for (Sample sample : smpday15Arr) {
                //得到1天前
                if(sample.getAddTime()>bfday1){
                    smpday1++;
                }
                //得到2天前
                if(sample.getAddTime()>bfday2){
                    smpday2++;
                }
                //得到3天前
                if(sample.getAddTime()>bfday3){
                    smpday3++;
                }
                //得到4天前
                if(sample.getAddTime()>bfday4){
                    smpday4++;
                }
                //得到5天前
                if(sample.getAddTime()>bfday5){
                    smpday5++;
                }
                //得到6天前
                if(sample.getAddTime()>bfday6){
                    smpday6++;
                }
                //得到7天前
                if(sample.getAddTime()>bfday7){
                    smpday7++;
                }
                //得到8天前
                if(sample.getAddTime()>bfday8){
                    smpday8++;
                }
                //得到9天前
                if(sample.getAddTime()>bfday9){
                    smpday9++;
                }
                //得到10天前
                if(sample.getAddTime()>bfday10){
                    smpday10++;
                }
                //得到11天前
                if(sample.getAddTime()>bfday11){
                    smpday11++;
                }
                //得到12天前
                if(sample.getAddTime()>bfday12){
                    smpday12++;
                }
                //得到13天前
                if(sample.getAddTime()>bfday13){
                    smpday13++;
                }
                //得到14天前
                if(sample.getAddTime()>bfday14){
                    smpday14++;
                }


            }





            //1天前
            //int ywcAddday1 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday2 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday3 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday4 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday5 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday6 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday7 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday8 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            // int ywcAddday9 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday10 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday11 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday12 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday13 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            //int ywcAddday14 = mReportDao.befaultxDateSh(bfday1);
            //1天前
            List<Report> ywcAddday15Arr = mReportDao.befaultxDateSh(bfday1);
            int ywcAddday1 =0;
            int ywcAddday2 =0;
            int ywcAddday3 =0;
            int ywcAddday4 =0;
            int ywcAddday5 =0;
            int ywcAddday6 =0;
            int ywcAddday7 =0;
            int ywcAddday8 =0;
            int ywcAddday9 =0;
            int ywcAddday10 =0;
            int ywcAddday11 =0;
            int ywcAddday12 =0;
            int ywcAddday13 =0;
            int ywcAddday14 =0;
            int ywcAddday15 =ywcAddday15Arr.size();
            for (Report report : ywcAddday15Arr) {
                if(report.getAddTime()>bfday1){
                    ywcAddday1++;
                }
                if(report.getAddTime()>bfday2){
                    ywcAddday2++;
                }
                if(report.getAddTime()>bfday3){
                    ywcAddday3++;
                }
                if(report.getAddTime()>bfday4){
                    ywcAddday4++;
                }
                if(report.getAddTime()>bfday5){
                    ywcAddday5++;
                }
                if(report.getAddTime()>bfday6){
                    ywcAddday6++;
                }
                if(report.getAddTime()>bfday7){
                    ywcAddday7++;
                }
                if(report.getAddTime()>bfday8){
                    ywcAddday8++;
                }
                if(report.getAddTime()>bfday9){
                    ywcAddday9++;
                }
                if(report.getAddTime()>bfday10){
                    ywcAddday10++;
                }
                if(report.getAddTime()>bfday11){
                    ywcAddday11++;
                }
                if(report.getAddTime()>bfday12){
                    ywcAddday12++;
                }
                if(report.getAddTime()>bfday13){
                    ywcAddday13++;
                }
                if(report.getAddTime()>bfday14){
                    ywcAddday14++;
                }
            }



            long sampCount = mSampleDao.getCount();
            long reportCount = mReportDao.getCount();


            long allreport =  mReportDao.getCountAll();
            long bhgReport = mReportDao.getCountBhq();



            //分页查询数据
            List<Sample> samples = mSampleDao.getSampleListPage(0, 10, "", "", 0, 0, 0,0, "", "","","");


            HomeBean homeBean = new HomeBean();
            //查询昨天新增的呼吸器数量
            homeBean.setHxqAdd1day(hxqAdd1day);
            //查询7日新增的呼吸器数量
            homeBean.setHxqAdd7day(hxqAdd7day);
            //查询14天前新增的呼吸器数量
            homeBean.setHxqAdd14day(hxqAdd14day);
            //查询30天前新增的呼吸器数量
            homeBean.setHxqAdd30day(hxqAdd30day);
            //查询30天前新增的呼吸器数量
            homeBean.setHxqAdd60day(hxqAdd60day);


            //查询昨天新增的复合气瓶数量
            homeBean.setFhqpAdd1day(fhqpAdd1day);
            //查询7日新增的复合气瓶数量
            homeBean.setFhqpAdd7day(fhqpAdd7day);
            //查询14天前新增的复合气瓶数量
            homeBean.setFhqpAdd14day(fhqpAdd14day);
            //查询30天前新增的复合气瓶数量
            homeBean.setFhqpAdd30day(fhqpAdd30day);
            //查询30天前新增的复合气瓶数量
            homeBean.setFhqpAdd60day(fhqpAdd60day);


            //查询昨天新增报告数量
            homeBean.setReportAdd1day(reportAdd1day);
            //查询7日新增的报告数量
            homeBean.setReportAdd7day(reportAdd7day);
            //查询30日新增的报告数量
            homeBean.setReportAdd30day(reportAdd30day);
            //查询30天前新增的报告数量
            homeBean.setReportAdd60day(reportAdd60day);


            homeBean.setYwcAdd1day(ywcAdd1day);
            //查询7日新增的已完成报告
            homeBean.setYwcAdd7day(ywcAdd7day);
             //查询30日新增已完成报告
            homeBean.setYwcAdd30day(ywcAdd30day);
            //查询30天前新增的已完成报告
            homeBean.setYwcAdd60day(ywcAdd60day);


            homeBean.setSampCount(sampCount);
            homeBean.setReportCount(reportCount);

            homeBean.setAllreport(allreport);
            homeBean.setBhgReport(bhgReport);


            List<String> dayStr15 = new ArrayList<>();
            dayStr15.add(day15);
            dayStr15.add(day14);
            dayStr15.add(day13);
            dayStr15.add(day12);
            dayStr15.add(day11);
            dayStr15.add(day10);
            dayStr15.add(day09);
            dayStr15.add(day08);
            dayStr15.add(day07);
            dayStr15.add(day06);
            dayStr15.add(day05);
            dayStr15.add(day04);
            dayStr15.add(day03);
            dayStr15.add(day02);
            dayStr15.add(day01);
            homeBean.setDayStr15(dayStr15);



            List<Integer> ypStr15 = new ArrayList<>();
            ypStr15.add(smpday15);
            ypStr15.add(smpday14);
            ypStr15.add(smpday13);
            ypStr15.add(smpday12);
            ypStr15.add(smpday11);
            ypStr15.add(smpday10);
            ypStr15.add(smpday9);
            ypStr15.add(smpday8);
            ypStr15.add(smpday7);
            ypStr15.add(smpday6);
            ypStr15.add(smpday5);
            ypStr15.add(smpday4);
            ypStr15.add(smpday3);
            ypStr15.add(smpday2);
            ypStr15.add(smpday1);
            homeBean.setYpStr15(ypStr15);


            List<Integer> bgStr15  = new ArrayList<>();
            bgStr15.add(ywcAddday15);
            bgStr15.add(ywcAddday14);
            bgStr15.add(ywcAddday13);
            bgStr15.add(ywcAddday12);
            bgStr15.add(ywcAddday11);
            bgStr15.add(ywcAddday10);
            bgStr15.add(ywcAddday9);
            bgStr15.add(ywcAddday8);
            bgStr15.add(ywcAddday7);
            bgStr15.add(ywcAddday6);
            bgStr15.add(ywcAddday5);
            bgStr15.add(ywcAddday4);
            bgStr15.add(ywcAddday3);
            bgStr15.add(ywcAddday2);
            bgStr15.add(ywcAddday1);
            homeBean.setBgStr15(bgStr15);

            List<Sample> samples2 =new ArrayList<>();
            samples2.addAll(samples);
            homeBean.setSamples(samples2);

            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(homeBean);

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return ret;
    }

}
